// pages/mine/address.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    bottomSize: 120,
    post: {},
    type: 0,    //1代表积分商城  2代表牛商城
    orderId: 0  //代表订单id  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { type = 0, orderId = 0 } = options;
    if(type) {
      this.setData({
        type: type,
        orderId: orderId
      })
    }
    this.scroll = this.selectComponent('.scroll');
  },
  onShow() {
    //启动刷新一次
    this.selectComponent('.scroll').startDown();
  },
  selectAddress(e) {
    if (this.data.type == 1) {
      let { id: address_id } = e.currentTarget.dataset;
      //这个代表返回积分商城
      app.http.$ajax({
        url: 'v1.leescore/setAddress',
        data: {
          orderid: this.data.orderId,
          address_id: address_id
        }
      }, {
        success:(res) => {
          app.success(res.msg);
          setTimeout(() => {
              wx.redirectTo({
                url: '/pages/order/order?orderId=' + this.data.orderId
              })
          })
        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  refresh(res) {
    this.setData({
      list: res.detail.data.data
    })
  },
  more(res) {
    this.data.list.push(...res.detail.data.data);
    this.setData({
      list: this.data.list
    })
  },

  navToAddrIns: function () {
    wx.navigateTo({
      url: '/pages/mine/edtAddr',
    })
  },
  edtAddr: function (e) {
    // console.log(e.currentTarget.id);
    var artid = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/mine/edtAddr?id=' + artid,
    })
  },
  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    this.scroll.more('more');
  },
})